/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.max.cjs.dao.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author user
 */
public class MySQLConnect {

    /**
     * Parametros de conexion
     */
    private String bd = "cjs";
    private final String login = "root";
    //private String password = "W@Web1233";
    private final String password = "W@Web1233";
    private final String url = "jdbc:mysql://localhost:3306/";

    Connection connection = null;
    /**
     * Constructor de DbConnection
     */
    public MySQLConnect() {
        try {
            //obtenemos el driver de para mysql
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        }
    }
    
    public MySQLConnect(String dataBaseName) {
        try {
            //obtenemos el driver de para mysql
            Class.forName("com.mysql.jdbc.Driver");
            bd = dataBaseName;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        }
    }
    
    public void openConnection() throws SQLException {
        if(connection== null) {
            connection = DriverManager.getConnection(url + bd, login, password);
        } else if( connection.isClosed()) {
            connection = DriverManager.getConnection(url + bd, login, password);
        }
    }
    
    public void closeConnection() throws SQLException {
        if(connection != null && !connection.isClosed()) {
            connection.close();
        }
    }

    /**
     * Permite retornar la conexión
     * @return 
     */
    public Connection getConnection() {
        return connection;
    }

    public void desconectar() {
        connection = null;
    }

}
